<?php
session_start();

require __DIR__ . '/../config.php';

if (Authentication::auth(hash('sha256', $pw))){
    // 鉴权为admin
    $_SESSION['user_auth'] = true;
    $auth = true;
}

if (isset($_GET['a']) && $_GET['c']){
    
    $action = $_GET['a']; // action
    $controller = $_GET['c']; // controller
    if (in_array($controller, $classNames) && method_exists($classList[$controller], $action)) {
        $cacheResult = Cache::get(json_encode($_GET));
        if ($cacheResult != false) {
            if ($cacheResult['type']=='json'){
                header('Content-Type: application/json');
            }else if ($cacheResult['type']=='html'){
                header('Content-Type: text/html');
            }
            die(json_encode(array('code' => $cacheResult['code'],'msg' => $cacheResult['msg'], 'data' => $cacheResult['data'])));
        }else{
            $httpData = $classList[$controller]->$action();
            
            die(json_encode(array('code' => $httpData['code'],'msg' => $httpData['msg'], 'data' => $httpData['data'])));
        }

    } else {
        echo json_encode(['code' => 404, 'msg' => '控制器不存在']);
    }

}